package cn.itcast.customview.Actrvity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import cn.itcast.customview.R;
import cn.itcast.customview.Util.DensityUtil;

public class ComboboxActivity extends AppCompatActivity {

    private EditText et_input;
    private ImageView iv_down_arrow;
    private PopupWindow popupWindow;
    private ListView listView;
    private ViewHold viewHold;
    private ArrayList<String> StringList;
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_combobox);

        et_input=(EditText)findViewById(R.id.et_input);
        iv_down_arrow=(ImageView)findViewById(R.id.iv_down_arrow);

        et_input.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (popupWindow==null){
                    popupWindow=new PopupWindow(ComboboxActivity.this);
                    popupWindow.setWidth(et_input.getWidth());

                    int height= DensityUtil.dip2px(ComboboxActivity.this,200);//dp->px
                    Toast.makeText(ComboboxActivity.this,"height="+height,Toast.LENGTH_SHORT).show();
                    popupWindow.setHeight(height);//px

                    popupWindow.setContentView(listView);
                    popupWindow.setFocusable(true);//设置焦点

                }
                popupWindow.showAsDropDown(et_input,0,0);

            }
        });
        listView=new ListView(ComboboxActivity.this);

        //准备数据
        StringList=new ArrayList<>();
        for (int i=0;i<50;i++){
            StringList.add(i+"----aaaaaaaaaaaaaaaaa----------"+i);

        }

        myAdapter=new MyAdapter();

        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                //1.得到数据
                String msg=StringList.get(position);
                //2.设置输入框
                et_input.setText(msg);

                if (popupWindow!=null&&popupWindow.isShowing()){
                    popupWindow.dismiss();
                    popupWindow=null;
                }

            }
        });
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return StringList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {

            if (convertView==null){
                convertView=View.inflate(ComboboxActivity.this,R.layout.item_main,null);
                viewHold=new ViewHold();
                viewHold.tv_msg=(TextView)convertView.findViewById(R.id.tv_msg);
                viewHold.iv_delete=(ImageView)convertView.findViewById(R.id.iv_delete);
                convertView.setTag(viewHold);
            }else{
                viewHold= (ViewHold) convertView.getTag();

            }
            //根据位置得到数据
            final String msg=StringList.get(position);
            viewHold.tv_msg.setText(msg);
            //设置删除
            viewHold.iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //1.从集合删除
                    StringList.remove(msg);

                    //2.刷新UI
                    myAdapter.notifyDataSetChanged();//getCount()-->getView();

                }
            });
            return convertView;
        }
    }
    static class ViewHold{
        TextView tv_msg;
        ImageView iv_delete;

    }
}
